const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    message_type:'',
    messageInfo:'',
    pageNum:1,
    next_page_url:'',
    loading:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      message_type: options.type
    })
    if (options.type == 1) {
      wx.setNavigationBarTitle({
        title: '系统消息'
      })
    } else if (options.type == 2) {
      wx.setNavigationBarTitle({
        title: '交易动态'
      })
    }/* else if (options.type == 3) {
      wx.setNavigationBarTitle({
        title: '收件箱'
      })
    } else {
      wx.setNavigationBarTitle({
        title: '发件箱'
      })
    }*/

  },
  toDetail(){

  },

  // 信息列表
  messageInfoList:function(){
    this.setData({
      loading:false
    })
    var _this = this
    var { message_type, messageInfo, pageNum, next_page_url } = this.data
    if (next_page_url === null){
      wx.showToast({
        title: '暂无更多',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    var url = app.globalData.baseUrl + 'user/Message_list';
    var data = {
      token: app.globalData.token,
      messageType: message_type,
      page: pageNum
    }
    wx.showLoading({
      title: '加载中',
    })
    app.wxGetRequest(url, data, (res) => {
      wx.hideLoading()

      console.log(res)
      if (res.code == 1000) {
        var messageInfoList = res.data.message_list.data
        const regex = new RegExp('<p style="(.*?)"|<p', 'gi');
        const regexp = new RegExp('<span style="(.*?)"|<span', 'gi');
        for (var i = 0; i < messageInfoList.length;i++){
          messageInfoList[i].message_content = messageInfoList[i].message_content.replace(regex,
              `<p style="color: #595959!important;
              font-size: 14px!important;
              line-height: 20px!important;
              white-space: nowrap;
              text-overflow: ellipsis;
              overflow: hidden;
              word-break: break-all;
              "`);
          messageInfoList[i].message_content = messageInfoList[i].message_content.replace(regexp, `<span style="color: #595959!important;font-size: 14px!important;line-height: 20px!important;"`);
        }
        _this.setData({
          messageInfo: [...messageInfo, ...messageInfoList],
          next_page_url: res.data.message_list.next_page_url
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        })
      }
      _this.setData({
        loading:_this.data.messageInfo.length===0
      })
    }, (err) => {
      wx.hideLoading()
    })
  },
  // 信息详情
  messageInfoDetail:function(e){
    var { id } = e.currentTarget.dataset
    var { message_type } = this.data
    wx.navigateTo({
      url: '/userPage/pages/dynamicInfo/dynamicInfo?id=' + id + '&message_type=' + message_type,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      pageNum:1,
      messageInfo:'',
      next_page_url:''
    })
    this.messageInfoList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var {pageNum} = this.data
    pageNum ++;
    this.setData({
      pageNum
    })
    this.messageInfoList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
